﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace BadHabit.Factory.Controls {
    /// <summary>
    /// Interaction logic for TaskFilter.xaml
    /// </summary>
    public partial class TaskToolBar : UserControl {


        

        public string OldProjectUId { get; private set; }

        // this is a key for the SearchText property        
        internal static readonly DependencyPropertyKey SearchTextKey =
            DependencyProperty.RegisterReadOnly("SearchText",
            typeof(String),
            typeof(TaskToolBar),
            new PropertyMetadata(null)
        );

        /// <summary>
        /// DP property for the SelectedBacklogItem.
        /// </summary>
        public static readonly DependencyProperty SearchTextProperty = SearchTextKey.DependencyProperty;

        /// <summary>
        /// Gets the selected project.
        /// </summary>
        public String SearchText {
            get { return (String)GetValue(SearchTextProperty); }
        }


        internal static readonly DependencyPropertyKey BugOnlyFilterKey =
            DependencyProperty.RegisterReadOnly("BugOnlyFilter",
            typeof(bool),
            typeof(TaskToolBar),
            new PropertyMetadata(null)
        );
        public static readonly DependencyProperty BugOnlyFilterProperty = BugOnlyFilterKey.DependencyProperty;
        public bool BugOnlyFilter {
            get { return (bool)GetValue(BugOnlyFilterProperty); }
        }

        internal static readonly DependencyPropertyKey ImpedimentOnlyFilterKey =
            DependencyProperty.RegisterReadOnly("ImpedimentOnlyFilter",
            typeof(bool),
            typeof(TaskToolBar),
            new PropertyMetadata(null)
        );
        public static readonly DependencyProperty ImpedimentOnlyFilterProperty = ImpedimentOnlyFilterKey.DependencyProperty;
        public bool ImpedimentOnlyFilter {
            get { return (bool)GetValue(ImpedimentOnlyFilterProperty); }
        }



        /// <summary>
        /// Sets if the toolbar is used at a post it panel.
        /// </summary>
        public bool IsPostItPanel {
            set {
                if (value) {
                    timer.Visibility = Visibility.Visible;
                    postItViewButton.Visibility = Visibility.Collapsed;                    
                    tableViewButton.Visibility = Visibility.Visible;                  
                    atTextBlock.Visibility = Visibility.Collapsed;

                    bugButton.Visibility = Visibility.Collapsed;
                    impedimentButton.Visibility = Visibility.Collapsed;

                    AddTaskMenu.Visibility = Visibility.Visible;

                    taskFilterComboBox.Visibility = Visibility.Collapsed;
                    taskFilterComboBox.SelectedIndex = 0; // at post it view, only daylly tasks
                } else {
                    timer.Visibility = Visibility.Collapsed;
                    timeSeparator.Visibility = Visibility.Collapsed;
                    postItViewButton.Visibility = Visibility.Visible;                   
                    tableViewButton.Visibility = Visibility.Collapsed;

                    bugButton.Visibility = Visibility.Visible;
                    impedimentButton.Visibility = Visibility.Visible;

                    AddTaskMenu.Visibility = Visibility.Collapsed;
                }
            }
            get {
                return (timer.Visibility == Visibility.Visible);
            }
        }

        private WPFTools.DelayFilter delayFilter;

        public TaskToolBar() {
            InitializeComponent();
            delayFilter = new BadHabit.WPFTools.DelayFilter(1, ApplyFilter);            
            
        }

        

        private void ApplyFilter() {
            SetValue(SearchTextKey, searchTextBox.Text);            
        }

        /// <summary>
        /// Gets the sprint number selected at the task filter.
        /// Zero means that no sprint was selected
        /// </summary>
        public short SprintNumberFilter {
            get {
                if (IsPostItPanel)
                    return 0;
                if (taskFilterComboBox.Items.Count == 0)
                    return 0;
                return (short)taskFilterComboBox.SelectedIndex;
            }
        }

        /// <summary>
        /// Returns true if "All project´s tasks" option was selected at the task filter.
        /// </summary>
        public bool IsAllSprintSelected {
            get {
                return (taskFilterComboBox.SelectedIndex == taskFilterComboBox.Items.Count-1);
            }
        }

        /// <summary>
        /// Updates the tsk filter items.
        /// </summary>
        /// <param name="sprintCount">The number of project sprints</param>
        public void UpdateTaskFilterComboItems() {

            Data.ProjectDataSet dsProject = DataContext as Data.ProjectDataSet;
            if (dsProject == null)
                return;

            int sprintCount = dsProject.Sprint.Count;

            String[] filterList = new String[sprintCount + 2];
            filterList[0] = Properties.Resources.Daily_tasks;

            for (int i = 1; i <= sprintCount; i++) {
                filterList[i] = "Sprint " + i + " " + Properties.Resources.tTasks;
            }
            filterList[sprintCount + 1] = Properties.Resources.Project_tasks;
            taskFilterComboBox.ItemsSource = filterList;

           

            if (dsProject.Project.Count == 0)
                return;

            // if the context change, changed the project select the DAILLY TASKS item
            // otherwise, it may be just a refresh, so keep the same selection
            if(!dsProject.Project[0].ProjectUId.Equals(OldProjectUId))
                taskFilterComboBox.SelectedIndex = 0;

            OldProjectUId = dsProject.Project[0].ProjectUId;

        }

        
        private void searchTextBox_SelectionChanged(object sender, RoutedEventArgs e) {
            delayFilter.StartFilter();
        }

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
            UpdateTaskFilterComboItems();            
        }

        private void bugButton_Click(object sender, RoutedEventArgs e) {
            SetValue(BugOnlyFilterKey, bugButton.IsChecked);            
        }

        private void impedimentButton_Click(object sender, RoutedEventArgs e) {
            SetValue(ImpedimentOnlyFilterKey, impedimentButton.IsChecked);            
        }
         
        private void teamAllocationButton_Click(object sender, RoutedEventArgs e) {

            Windows.TeamAllocation w;

            Data.ProjectDataSet dsProject = DataContext as Data.ProjectDataSet;
            if (dsProject == null) 
                w = new Windows.TeamAllocation();
            else
                w = new Windows.TeamAllocation(dsProject.Project[0].ProjectNumber);

            w.Show();
                

            
        }
        /* I NEED A ENGLISH VISTA TO TEST THIS THING
         * 
         * 
         
        private SpeechRecognitionEngine speechRecognizer;
        private void talkingMenuItem_Click(object sender, RoutedEventArgs e) {            
            speechRecognizer.RecognizeAsync();
        }

        void speechRecognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) {
            MessageBox.Show(e.Result.Text);
        }

        private void SelectRecognitionEngine() {                        
            foreach (System.Speech.Recognition.RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers()) {
                if (config.Culture.Name.Equals("pt-BR")) {
                    speechRecognizer = new SpeechRecognitionEngine(config);
                    speechRecognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(speechRecognizer_SpeechRecognized);                    
                    speechRecognizer.SetInputToDefaultAudioDevice();
                    LoadGrammar();                                        
                    break;
                }                
            }
 
        }

        private void LoadGrammar() {
            System.Speech.Recognition.Choices choices = new Choices();
            choices.Add("teste");
            GrammarBuilder gramaBuilder = new GrammarBuilder(choices);
            Grammar grammar = new Grammar(choices);
            speechRecognizer.LoadGrammar(grammar);
        }
        */


    }
}
